package me.sdevil507.org.controller;

import lombok.extern.slf4j.Slf4j;
import me.sdevil507.org.dto.*;
import me.sdevil507.org.mapper.OrgResourceMapper;
import me.sdevil507.org.po.OrgDeptPo;
import me.sdevil507.org.po.OrgResourcePo;
import me.sdevil507.org.po.OrgRoleResourceRelPo;
import me.sdevil507.org.service.OrgResourceService;
import me.sdevil507.org.service.OrgRoleService;
import me.sdevil507.platform.dto.PlatResourceModuleResultDto;
import me.sdevil507.platform.dto.PlatResourceResultDto;
import me.sdevil507.platform.mapper.PlatResourceMapper;
import me.sdevil507.platform.po.PlatResourcePo;
import me.sdevil507.platform.service.PlatResourceService;
import me.sdevil507.supports.jpa.PageConvertCallBack;
import me.sdevil507.supports.jpa.PageResultDTO;
import me.sdevil507.supports.jpa.PageResultGenerator;
import me.sdevil507.supports.result.ApiResultDTO;
import me.sdevil507.supports.result.ApiResultGenerator;
import me.sdevil507.supports.status.ApiStatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 机构资源表[OrgResourceController]
 *
 * @author sdevil507
 * created on 2020-06-23 18:43:30
 */
@Controller
@RequestMapping("/api/org/resource")
@Slf4j
public class OrgResourceController {

    @Autowired
    private OrgResourceService orgResourceService;

    @Autowired
    private OrgRoleService orgRoleService;

    @Autowired
    private OrgResourceMapper orgResourceMapper;

    @Autowired
    private PlatResourceService platResourceService;

    @Autowired
    private PlatResourceMapper platResourceMapper;

    /**
     * 获取资源树
     *
     * @return 信息列表
     */
    @RequestMapping(value = "/readAll", method = RequestMethod.GET)
    @ResponseBody
    public ApiResultDTO readAll() {
//        List<PlatResourceResultDto> list = platResourceMapper.pos2dtos(orgResourceService.readOrgAllModule());
        //查询当前登录人所有授权的模块
//        List<OrgRoleResourceRelPo> orgModules = orgRoleService.findPermissions();
        //所有模块
        List<PlatResourceModuleResultDto> list = platResourceMapper.pos2ModuleDtos(platResourceService.readAll());
//        //取交集
//        List<PlatResourceModuleResultDto> list = resourcePoList.stream().filter(platResourcePo ->
//                orgModules.stream().anyMatch(orgRoleResourceRelPo -> orgRoleResourceRelPo.getId().equals(platResourcePo.getId()))
//        ).collect(Collectors.toList());

        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), list);
    }

    /**
     * 获取资源模块树(包含按钮)
     *
     * @return 信息列表
     */
    @RequestMapping(value = "/readAllWithBtn", method = RequestMethod.GET)
    @ResponseBody
    public ApiResultDTO readAllWithBtn() {
        //所有模块
        List<OrgResourceModuleResultDto> list = orgResourceMapper.pos2ModuleDtos(orgResourceService.readAll());
        //已授权模块
        List<String> haveModuleList = orgResourceService.readOrgAllModule().stream().map(PlatResourcePo::getCode).collect(Collectors.toList());
        //循环比较授权
        this.matchModule(list.get(0).getSubModules(), haveModuleList);

        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), list.get(0).getSubModules());
    }

    private void matchModule(List<OrgResourceModuleResultDto> subModules, List<String> haveModuleList){
        if(!CollectionUtils.isEmpty(subModules)){
            Iterator<OrgResourceModuleResultDto> it = subModules.iterator();
            while (it.hasNext()) {
                OrgResourceModuleResultDto ord = it.next();
                if(ord == null) continue;
                if(haveModuleList.contains(ord.getCode())){
                    matchModule(ord.getSubModules(), haveModuleList);
                }else{
                    it.remove();
                }
            }
        }
    }

    /**
     * 分页获取信息列表
     *
     * @param dto 聚合参数
     * @return 分页数据
     */
    @SuppressWarnings("Convert2Lambda")
    @RequestMapping(value = "/readAllByPage", method = RequestMethod.POST)
    @ResponseBody
    public ApiResultDTO readAllByPage(@RequestBody OrgResourceMergeParamsDto dto) {
        Page<OrgResourcePo> pagePos = orgResourceService.readAllByPage(dto.getSearchDto(), dto.getPageDto(), dto.getSortDto());
        PageResultDTO<OrgResourceSimpleResultDto> pageResultDto = PageResultGenerator.convert(pagePos, new PageConvertCallBack<OrgResourceSimpleResultDto, OrgResourcePo>() {
            @Override
            public List<OrgResourceSimpleResultDto> exec(List<OrgResourcePo> list) {
                return orgResourceMapper.pos2SimpleDtos(list);
            }
        });
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), pageResultDto);
    }

    /**
     * 创建时获取模块编码
     *
     * @param dto 入参
     * @return 模块编码
     */
    @RequestMapping(value = "/getModuleCode", method = RequestMethod.POST)
    @ResponseBody
    public ApiResultDTO getModuleCode(@RequestBody OrgResourceParamsDto dto) {
        String moduleCode = orgResourceService.getModuleCode(dto.getParentId());
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), moduleCode);
    }

    /**
     * 创建
     *
     * @param dto 传输类
     * @return 执行反馈
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public ApiResultDTO create(@RequestBody OrgResourceParamsDto dto) {
        orgResourceService.create(dto);
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
    }

    /**
     * 更新
     *
     * @param dto 传输类
     * @return 执行反馈
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ApiResultDTO update(@RequestBody OrgResourceParamsDto dto) {
        orgResourceService.update(dto);
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
    }

    /**
     * 删除
     *
     * @param dto 传输类
     * @return 执行反馈
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ApiResultDTO delete(@RequestBody OrgResourceParamsDto dto) {
        orgResourceService.delete(dto);
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
    }
}